package com.market.controller;


import com.market.common.exception.ControllerException;
import com.market.common.vo.ResponseCode;
import com.market.common.vo.ResponseResult;
import com.market.entity.Units;
import com.market.service.UnitsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/units")
public class UnitsController {
    @Resource
    UnitsService unitsService;

    /**
     * 添加
     * @param unitsName
     * @return
     */
    @GetMapping("/add")
    public ResponseResult<Integer> add(@RequestParam String unitsName) {
        int addUnits = unitsService.addUnits(unitsName);
        return ResponseResult.success(ResponseCode.SUCCESS, addUnits);
    }

    /**
     * 删除
     * @param unitsId
     * @return
     * @throws ControllerException
     */
    @GetMapping("/delete")
    public ResponseResult<Integer> del(@RequestParam Integer unitsId) throws ControllerException {
        int delete = unitsService.deleteUnits(unitsId);
        return ResponseResult.success(ResponseCode.SUCCESS, delete);
    }

    /**
     * 修改
     * @param untisId
     * @param unitsName
     * @return
     * @throws ControllerException
     */
    @PostMapping("/changer")
    public ResponseResult<Integer> update(@RequestParam Integer untisId, @RequestParam String unitsName) throws ControllerException {
        int change = unitsService.update(untisId, unitsName);
        return ResponseResult.success(ResponseCode.SUCCESS, change);
    }

    /**
     * 单位搜索功能
     *
     * @param namesOrId
     * @return
     * @throws ControllerException
     */
    @GetMapping("/search")
    public ResponseResult<List<Units>> search(@RequestParam String namesOrId) throws ControllerException {
        List<Units> units = unitsService.searchClasses(namesOrId);
        return ResponseResult.success(ResponseCode.SUCCESS, units);
    }
}
